#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/17 15:36
# @File    : 123.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn

import tkinter as tk
from tkinter import ttk


class Ui:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('gui')
        self.root.geometry("600x500")
        self.interface()

    def interface(self):
        style = ttk.Style()
        # 数据字体大小
        style.configure("Treeview", rowheight=50, font=("Arial", 15, 'bold'))
        #  # 设置标题字体
        style.configure("Treeview.Heading", font=("Arial", 12, 'bold'))

        self.tree = ttk.Treeview(self.root, height=10, style='Treeview')
        self.tree.grid(row=0, column=0, rowspan=10, columnspan=10, padx=60)


        # 列名
        self.tree['columns'] = ("Name", "Age", "City")

        # 设置标题名
        self.tree.heading("#0", text='序号', anchor='w')  # #0是默认索引号，标题文本为序号
        self.tree.heading("Name", text='姓名', anchor='w')
        self.tree.heading("Age", text='年龄', anchor='w')
        self.tree.heading("City", text='城市', anchor='w')

        # 5设置列宽度（像素），无anchor参数时，列表数据清楚时，默认左对齐#0除外。
        self.tree.column("#0", width=100)
        self.tree.column("Name", width=100)
        self.tree.column("Age", width=90)
        self.tree.column("City", width=150)

        # 插入默认数据
        self.tree.insert("", tk.END, text='1', values=('轩萱', 9, "河南郑州"))
        self.tree.insert("", tk.END, text='1', values=('宝水', 12, "河南郑州"))




        # 创建按钮
        self.btn0 = tk.Button(self.root, text='获取数据', command=self.get_data)
        self.btn0.grid(row=11, column=0, ipadx=10, pady=20)

        self.btn1 = tk.Button(self.root, text="获取全部数据", command=self.get_all_data)
        self.btn1.grid(row=11, column=1, ipadx=10, pady=20)

        # 添加文本框
        self.label = tk.Label(self.root, text='打印日志', font=("宋体", 10), bg='green')
        self.label.grid(row=12, column=0, ipadx=10, pady=10)

        self.w1 = tk.Text(self.root, width=50, height=5)
        self.w1.grid(row=13, column=0, columnspan=5, padx=60)

    def get_data(self):
        '''获取选中数据'''
        selected_items = self.tree.selection()
        if selected_items:
            for item in selected_items:
                row_data = self.tree.item(item)['values']
                self.w1.insert("1.0", f'selected Row Data:{row_data}\n')
        else:
            self.w1.insert("1.0", "未选中任何数据\n")

    def get_all_data(self):
        rows = self.tree.get_children()  # 获取所有行的Id
        # 遍历每一行
        for row in rows:
            # 获取该行字典
            data = self.tree.item(row)
            self.w1.insert('1.0', f"{data['values']}\n")


if __name__ == '__main__':
    app = Ui()
    app.root.mainloop()